﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ReceiveGroupData
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            GetData getData = new GetData(10002);
            thGetData = new Thread(new ThreadStart(getData.ReceiveData));
            thGetData.IsBackground = true;

            btnStart.Click += new EventHandler(btn_Click);
            btnStop.Click += new EventHandler(btn_Click);
            btnStart.Enabled = true;
            btnStop.Enabled = false;


            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Interval = 200;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Enabled = true;
        }

        void btn_Click(object sender, EventArgs e)
        {
            Button btn=sender as Button;
            if (btn.Name == btnStart.Name)
            {
                btn.Enabled = false;
                btnStop.Enabled = true;
                thGetData.Start();
            }
            else
            {
                btn.Enabled = false;
                btnStart.Enabled = true;
                thGetData.Abort();
            }
        }
        private Thread thGetData = null;
        /// <summary>
        /// 错误信息显示时间间隔
        /// </summary>
        private const int iErrInfoDlg = 30;
        private DateTime dt = DateTime.Now;
        void timer_Tick(object sender, EventArgs e)
        {
            lblTime.Text = string.Format("待处理数据包：{0} 当前时间：{1}", CGlobal.GetWaitCount(), DateTime.Now);
            
            //……
            int iPos = CGlobal.GetWaitCount() / 2;
            if (iPos < 1) iPos = 1;
            if (CGlobal.GetWaitCount() == 0) return;
            List<DGZone> tmp = new List<DGZone>();
            CGlobal.unPack(iPos, ref tmp);
            if (tmp.Count <= 0) return;
            this.Refresh();
        }        
    }        
}
